package com.dq.controller.app.org;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dq.controller.in.org.OrgQuery;
import com.dq.domain.org.Org;
import com.dq.exception.customException.AdminException;
import com.dq.service.AllService;
import com.dq.vo.ListDataVo;
import com.dq.vo.Vo;
import com.dq.vo.baseVo.VO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author ----
 * @date 2019/9/1821:29
 */
@Api(description = "门店管理")
@RestController
@RequestMapping("/app/org/")
public class AppOrgController extends AllService {
    @GetMapping("listAll")
    @ApiOperation("获取 所有门店-不分页")
    public Vo getListAll() throws AdminException {
        List<Org> orgList = orgService.getListAll();

        JSONArray array = new JSONArray();
        for (Org org : orgList) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", org.getId());
            jsonObject.put("name", org.getSimpleName());
            array.add(jsonObject);
        }

        return new ListDataVo(array);
    }
    /**
     * 获取 负责区域内 门店
     */
    @GetMapping("listAllByUser")
    @ApiOperation("获取负责的门店列表 - 不分页")
    public Vo getListAllByUser() throws AdminException {
        List<Org> orgList = orgService.getListAllByUser();

        JSONArray array = new JSONArray();
        for (Org org : orgList) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("key", org.getId());
            jsonObject.put("display_name", org.getSimpleName());
            array.add(jsonObject);
        }

        return new ListDataVo(array);
    }
    /**
     * 获取 负责区域内 门店
     */
    @PostMapping("listByUser")
    @ApiOperation("获取负责的门店列表 - 分页")
    public Vo getListByUser(@RequestBody OrgQuery orgQuery) throws AdminException {
        orgQuery.format();

        List<Org> list = orgService.listByLogin(orgQuery);
        return new ListDataVo((List<Org.Vo>) VO.toVOCollection(list), orgService.counteByLogin(orgQuery));
    }

    @PostMapping("/add")
    @ApiOperation("门店添加")
    public Vo add(@RequestBody @Valid Org org) throws AdminException {
        Org save = orgService.save(org);

        return new Vo(save.toVO());
    }

    @PostMapping("/edit/{orgId}")
    @ApiOperation("门店信息修改")
    public Vo edit(@PathVariable Long orgId, @RequestBody Org orgEdit)throws AdminException {
        orgEdit.setId(orgId);
        Org update = orgService.update(orgEdit);
        return new Vo(update.toVO());
    }

    /****
     * 删除门店， 同时删除门店账号
     */
    @GetMapping("/delete/{orgId}")
    @ApiOperation("删除门店信息")
    public Vo delete(@PathVariable Long orgId)throws AdminException{
        sysSectionService.delete(orgId);

        return new Vo(HttpStatus.OK);
    }

}